package com.shengqi.xinnongren.work.ui.user.activities;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.adapter.GeneralFragmentPagerAdapter;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.ui.user.fragments.CouponFragment;
import com.shengqi.xinnongren.work.ui.user.model.CouponNumBean;
import com.shengqi.xinnongren.work.ui.user.presenter.MyCouponPresenter;
import com.shengqi.xinnongren.work.ui.user.view.MyCouponView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的优惠券
 */
public class MyCouponActivity extends BaseMvpActivity<MyCouponView, MyCouponPresenter> implements MyCouponView {

    @BindView(R.id.tl_coupon)
    TabLayout mTabLayout;
    @BindView(R.id.vp_coupon)
    ViewPager mViewPager;

    private List<String> mTitles;
    private List<Fragment> mFragments;
    private String[] types = {"1", "2", "3"};
    private GeneralFragmentPagerAdapter mAdapter;

    @Override
    protected MyCouponPresenter createPresenter() {
        return new MyCouponPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_my_coupon;
    }

    @Override
    protected void initView() {
        mTitles = new ArrayList<>();
        mTitles.add("未使用");
        mTitles.add("已使用");
        mTitles.add("已过期");
        mFragments = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            CouponFragment couponFragment = new CouponFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type", types[i]);
            couponFragment.setArguments(bundle);
            mFragments.add(couponFragment);
        }
        mAdapter = new GeneralFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(mTitles.size());
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected void initData() {
        mPresenter.getCouponNum();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {

    }

    @Override
    public void showCouponNum(CouponNumBean bean) {

        mAdapter.notifyTitle(0, "未使用(" + bean.getUseSum() + ")");
        mAdapter.notifyTitle(1, "已使用(" + bean.getIsUseSum() + ")");
        mAdapter.notifyTitle(2, "已过期(" + bean.getExpSum() + ")");
    }

    public void updateCouponNum() {
        mPresenter.getCouponNum();
    }

}
